package fragment;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dong.adapter.Adapter_list1;
import com.dong.adapter.ViewPager2Adapter;
import com.dong.utils.Constants;
import com.dong.utils.Uri2String;
import com.dong.view.MyListView;
import com.dong.yi18health.R;

import android.R.color;
import android.app.Notification.Style;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.TextView;
import android.widget.Toast;


public class fragment_title1 extends Fragment {

	private View view;
	private List<News> datas;
	ArrayList<Fragment> data = new ArrayList<>();
	private List<News2> d = new ArrayList<>();
	private ViewPager pager;
	private Adapter_list1 ad;
	int index = 1;
	boolean f=false;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.base_item1, null);
		initView();
		praseUrl();
		return view;
	}

	// 处理listView数据并填充
	Handler h = new Handler() {

		public void handleMessage(Message msg) {
			String result = (String) msg.obj;
			JSONObject o = JSON.parseObject(result);
			String s = o.getString("yi18");
			List<News2> da = JSON.parseArray(s, News2.class);
			d.addAll(da);
			ad.notifyDataSetChanged();
		};
	};
	
//ListView的数据下载
	private void initListData() {
		new Thread() {
			public void run() {
				String result2 = Uri2String.getResult("http://api.1ccf.com/news/list?page=" + index + "&limit=10");
				pd.cancel();
				//创建或者复用已有的message
				Message m = Message.obtain();
				m.obj = result2;
				h.sendMessage(m);
			};
		}.start();
	}
	//数据更新时的方法
	private void initListData2() {
		index = index + 1;
		new Thread() {
			public void run() {
				String path="http://api.1ccf.com/news/list?page=%d&limit=10";
				String url = String.format(path, index);
				String result2 = Uri2String.getResult(url);
				pd.cancel();
				//创建或者复用已有的message
				Message m = Message.obtain();
				m.obj = result2;
				h.sendMessage(m);
			};
		}.start();
	}
	//控件的初始化
		private void initView() {
			pager = (ViewPager) view.findViewById(R.id.baseitem1_pager);
			lv = (MyListView) view.findViewById(R.id.baseitem1_list);
			
			//创建提示框
			pd = new ProgressDialog(getActivity());
			pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			pd.setTitle("加载页面");
			pd.setMessage("正在下载...");
			pd.show();
			
			//给ListView加根
			View foot = View.inflate(getActivity(), R.layout.item_listtoast, null);
			lv.addFooterView(foot);
			
			//ListView的下载任务
			initListData();
				
			//创建适配器
			ad = new Adapter_list1(d, getActivity());
			
			//给ListView加适配器
			lv.setAdapter(ad);
			
			// listView加滑动监听 到底部时加载新的数据并显示提示对话框
			lv.setOnScrollListener(new OnScrollListener() {
				
				@Override
				public void onScrollStateChanged(AbsListView arg0, int arg1) {
					if(arg1==OnScrollListener.SCROLL_STATE_IDLE&&f==true)
					// 重新加载数据
					initListData2();
					f=false;
				}
				
				@Override
				public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
					if (arg1 + arg2 == arg3) {
						f=true;
					}
				}
			});
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
// 数据初始化为ViewPager准备数据
	private void initData() {

		for (int i = 0; i < datas.size(); i++) {
			News news = datas.get(i);
			itemFragment it = new itemFragment();
			it.setNews(news);
			data.add(it);
		}
		// 给ViewPager填充数据
		pager.setAdapter(new ViewPager2Adapter(getActivity().getSupportFragmentManager(), data));
		// 加自动滑动
		scorllViewPager();
	}
//处理ViewPager自动滑动的处理者
	Handler hand = new Handler() {
		int i = 0;

		public void handleMessage(android.os.Message msg) {

			pager.setCurrentItem(i);
			i++;
			if (i == datas.size()) {
				i = 0;
			}
			hand.sendEmptyMessageDelayed(1, 3000);
		};
	};
	private MyListView lv;
	private ProgressDialog pd;
//开启滑动
	private void scorllViewPager() {
		hand.sendEmptyMessage(0);

	}
//异步任务完成ViewPager数据的网络下载 并解析得到数据 并初始化
	private void praseUrl() {

		new b().execute(Constants.TOP_URL);

	}

	// 完成下载和解析任务
	class b extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... arg0) {

			return Uri2String.getResult(arg0[0]);
		}
//在子线程的下载完成后执行解析和数据的初始化
		@Override
		protected void onPostExecute(String result) {
			JSONObject o = JSON.parseObject(result);
			String s = o.getString("yi18");
			datas = JSON.parseArray(s, News.class);
			initData();
		}
	}

}
